package com.yangjun.service.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yangjun.common.enums.report.AnalysisTypeEnum;
import com.yangjun.common.enums.report.OutstockTypeEnum;
import com.yangjun.dao.mybatis.mapper.usermanage.UserManageMapper;
import com.yangjun.dao.mybatis2.mapper.common.CommonMapper;
import com.yangjun.entity.common.PlatformEntity;
import com.yangjun.entity.common.PlatformShopEntity;
import com.yangjun.service.permission.ReportExportPermissionService;
import com.yangjun.dao.mybatis.mapper.usermanage.ExcelRoleManagerMapper;
@Service
public class CommonService {
	@Autowired
	public CommonMapper mapper;
	@Autowired
	private UserManageMapper userManageMapper;
	@Autowired
	private ReportExportPermissionService reportExportPermissionService;
	@Autowired
	private ExcelRoleManagerMapper excelRoleManagerMapper;
	
	public List<Map<String,Object>> HandleCheackBoxStatus(List<String> cheackBoxNameList,String[] selectData){
		List<Map<String,Object>> resultList = Lists.newArrayList();
		if(cheackBoxNameList!=null&&cheackBoxNameList.size()>0){
			Map<String,Object> itemMap = null;
			for(String cheackBoxName : cheackBoxNameList){
				itemMap = Maps.newHashMap();
				itemMap.put("name", cheackBoxName);
				if(selectData!=null&&selectData.length>0){
					itemMap.put("checked", false);
					for(String selectItem : selectData){
						if(cheackBoxName.equals(selectItem)){
							itemMap.put("checked", true);
							break;
						}
					}
				}else{
					itemMap.put("checked", true);
				}
				resultList.add(itemMap);
			}
		}
		return resultList;
	}
	
	public List<String> getOrderAnalysisType(String userid,String reporttype){
		 List<String> resultList = Lists.newArrayList();
		List<String> dimList = reportExportPermissionService.getUserExcelDimSet(userid, reporttype);
		for(AnalysisTypeEnum analysisTypeEnum : AnalysisTypeEnum.values()){
			String name = analysisTypeEnum.getName();
			if(dimList.contains(name)){
				resultList.add(analysisTypeEnum.getValue());
			}
		}
		return resultList;
	}
	
	/**
	 * 
	 * @Description 根据用户限制类型（1是不限制的超级管理员账号，0是商品经理账号），用户ID，报表类型查询平台
	 * @param type
	 * @param userid
	 * @param reporttype
	 * @return
	 * @author yangjun
	 * @Date 2017年9月25日 上午11:09:10
	 */
	public List<Map<String, Object>> getOrderSource(int type,String userid,String reporttype){
		
		List<Map<String, Object>> ordersource = Lists.newArrayList();
		//20190711新增，俏林说要新增一个角色，自动获取全平台，优先判断
		List<Integer> roidList = excelRoleManagerMapper.getUserRoleIdByUserId(userid);
		//select * from  dv_kad_excel_role where rolename_ch='平台全选' ;663是平台全选这个角色
		if(roidList.contains(663)){
			return getPlatformList();
		}
		//0是订阅报表的要获取商品经理名下的平台1是没订阅的平台为全部
				if(type==0){
					ordersource = getSaleManagerPlatformList(userManageMapper.getCurrentUsernameById(userid));
				}else if(type==1){
					//只针对不限制的用户进行平台的控制
					List<String> dimList = reportExportPermissionService.getUserExcelDimSet(userid, reporttype);
					ordersource = getPlatformList();
					if(ordersource!=null){
					    Iterator<Map<String, Object>> it = ordersource.iterator();    
					    while(it.hasNext()){  
							Map<String, Object> ordersourceItem = (Map<String, Object>) it.next();
							String sourcename = ordersourceItem.get("sourcename").toString();
							if(dimList!=null&&!dimList.contains(sourcename)){
								it.remove();
							}
					    }
					}
				}
				return ordersource;
	}
	public List<String> getDim(String userid,String reporttype,List<String> originDimList){
		List<String> resultList = Lists.newArrayList();
		List<String> dimList = reportExportPermissionService.getUserExcelDimSet(userid, reporttype);
		for(String originDim : originDimList){
			if(dimList!=null&&dimList.contains(originDim)){
				resultList.add(originDim);
			}
		}
		return resultList;
	}
	public List<String> getActiveType(){
		return mapper.getActiveType();
	}
	public List<String> getOrgname(){
		return mapper.getOrgname();
	}
	public Map<String, Object> getSalesPlatform(){
		Map<String, Object> attributes = Maps.newHashMap();
		attributes.put("platform", getPlatformList());
		return attributes;
	}
	public List<Map<String, Object>> getPlatformList(){
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		StringBuffer other = new StringBuffer();
		List<Map<String, Object>> showPlatformList = new ArrayList<Map<String, Object>>();
//		List<String> siteidWeight = Arrays.asList(DVConstants.SITEID_WEIGHT);
		List<String> siteidWeight = mapper.lastHaveSalesPlatform();
		List<PlatformEntity> list = mapper.getSalesPlatform(parameterMap);
		for (PlatformEntity entity : list) {
			if (siteidWeight.contains(entity.getOrdersource().toString())) {
				HashMap<String, Object> showPlatform = Maps.newHashMap();
				if ("1".equals(entity.getOrdersource().toString())) {
					// 客服代下也算PC网站
					showPlatform.put("ordersource", "1,2");
				} else {
					showPlatform.put("ordersource", entity.getOrdersource());
				}
				if(entity.getOrdersource()==53){
					showPlatform.put("sourcename", "E+药房(零售)");
				}else if(entity.getOrdersource()==99902){
					showPlatform.put("sourcename", "E+药房(批发)");
				}else{
					showPlatform.put("sourcename", entity.getSourcename());
				}
				showPlatformList.add(showPlatform);
			} else {
				// 2已经添加到pc 无需再添加到其它
				if (!"2".equals(entity.getOrdersource().toString())) {
					other.append("," + entity.getOrdersource());
				}
			}
		}

		HashMap<String, Object> otherPlatform = Maps.newHashMap();
		otherPlatform.put("ordersource", other.substring(1));
		otherPlatform.put("sourcename", "其它");
		showPlatformList.add(otherPlatform);
		return showPlatformList;
	}
	public Map<String, Object> getSaleManagerPlatform(String loginname){
		Map<String, Object> attributes = Maps.newHashMap();
		attributes.put("platform", getSaleManagerPlatformList(loginname));
		return attributes;
	}
	public List<Map<String, Object>> getSaleManagerPlatformList(String loginname){
		HashMap<String, Object> parameterMap = Maps.newHashMap();
		parameterMap.put("loginname", loginname);
		StringBuffer other = new StringBuffer();
		List<Map<String, Object>> showPlatformList = new ArrayList<Map<String, Object>>();
//		List<String> siteidWeight = Arrays.asList(DVConstants.SITEID_WEIGHT);
		List<String> siteidWeight = mapper.lastHaveSalesPlatform();
		List<PlatformEntity> list = mapper.getSaleManagerPlatform(parameterMap);
		for (PlatformEntity entity : list) {
			if (siteidWeight.contains(entity.getOrdersource().toString())) {
				HashMap<String, Object> showPlatform = Maps.newHashMap();
				if ("1".equals(entity.getOrdersource().toString())) {
					// 客服代下也算PC网站
					showPlatform.put("ordersource", "1,2");
				} else {
					showPlatform.put("ordersource", entity.getOrdersource());
				}
				if(entity.getOrdersource()==53){
					showPlatform.put("sourcename", "E+药房(零售)");
				}else if(entity.getOrdersource()==99902){
					showPlatform.put("sourcename", "E+药房(批发)");
				}else{
					showPlatform.put("sourcename", entity.getSourcename());
				}
				showPlatformList.add(showPlatform);
			} else {
				// 2已经添加到pc 无需再添加到其它
				if (!"2".equals(entity.getOrdersource().toString())) {
					other.append("," + entity.getOrdersource());
				}
			}
		}

		if(other!=null && other.toString().length()>0){
			
			HashMap<String, Object> otherPlatform = Maps.newHashMap();
			otherPlatform.put("ordersource", other.substring(1));
			otherPlatform.put("sourcename", "其它");
			showPlatformList.add(otherPlatform);
		}
		return showPlatformList;
	}
	
	public List<PlatformShopEntity> getShopOfOrdersource(Map<String, Object> parameter){
		return mapper.getShopOfOrdersource(parameter);
	}
}
